<?php

class User extends BaseModel
{
    CONST AUTHTYPE_ALL          = -1;
    CONST AUTHTYPE_GUEST        = 1;
    CONST AUTHTYPE_MEMBER       = 2;
    CONST AUTHTYPE_WORKER       = 3;
    CONST AUTHTYPE_MANAGER      = 11;
    CONST AUTHTYPE_BOSS         = 12;
    CONST AUTHTYPE_ADMIN        = 99;

    CONST STATUS_ALL            = -1;
    CONST STATUS_OPEN           = 1;
    CONST STATUS_CLOSE          = 2;
    CONST STATUS_DELETE         = 9;

    CONST EMAILSTATUS_ALL       = -1;
    CONST EMAILSTATUS_SUCCESS   = 1;
    CONST EMAILSTATUS_NOTHING   = 2;
    CONST EMAILSTATUS_FAIL      = 3;

    /*
        讀取資料表而產生, 請視同cache, 任何情況下皆不可更動該產生的內容
    */
    public static function getTableDefinition()
    {
    }

    public function getTableFields() {
        return Array(
            "id"            => "getId",
            "account"       => "getAccount",
            "password"      => "getPassword",
            'auth_method'   => 'getAuthMethod',
            'auth_type'     => 'getAuthType',
            'email'         => 'getEmail',
            'email_status'  => 'getEmailStatus',
            'status'        => 'getStatus',
            'create_date'   => 'getCreateDateByDb',
            'update_date'   => 'getUpdateDateByDb',
            'properties'    => 'getProperties',
        );
    }

    public function validate()
    {
        $validateInfo = parent::validate();

        $result = preg_match( '/^[a-z][a-z0-9_]+$/i', $this->getAccount() );
        if(!$result) {
            $validateInfo['account']['message'] = '帳號格式不正確';
        }

        // email validate
        $result = filter_var( $this->getEmail(), FILTER_VALIDATE_EMAIL );
        if(!$result) {
            $validateInfo['email']['message'] = 'Email格式不正確';
        }

        // status validate
        $result = false;
        foreach( $this->getStatusList('status') as $name => $id ) {
            if( $this->getStatus()==$id ) {
                $result = true;
                break;
            }
        }
        if(!$result) {
            $validateInfo['status']['message'] = 'status 不正確';
        }

        /*
            建議使用
            // IntegerValidate();
            // EmailValidate();
            // NotHtmlValidate();

            $result = Array();
            if( !ValidateManager::pregMatchValidate( $model->getAccount() , $canNull=false , $rule='/^[a-zA-Z0-9]+$/' ) ) {
                $result['account'] = '帳號只能使用英文或數字';
            }
        */
        return $validateInfo;
    }
    public function filter()
    {
        $this->setAccount    ( trim(strip_tags($this->getAccount()    )) );
        $this->setAuthMethod ( trim(strip_tags($this->getAuthMethod() )) );
        $this->setEmail      ( filter_var($this->getEmail(), FILTER_SANITIZE_EMAIL) );
    }

    public function resetValue() {
        $this->__sleep();
        $this->setId            ( -1 );
        $this->setAccount       ( NULL );
        $this->setPurePassword  ( NULL );
        $this->setAuthMethod    ( 'basic' );
        $this->setAuthType      ( User::AUTHTYPE_GUEST );
        $this->setEmail         ( NULL );
        $this->setEmailStatus   ( User::EMAILSTATUS_NOTHING );
        $this->setStatus        ( User::STATUS_OPEN );
        $this->setCreateDate    ( time() );
        $this->setUpdateDate    ( time() );
        $this->setProperties    ( Array() );
    }





    public function setAccount($string) {        $this->_account = trim($string);  }
    public function getAccount()        { return $this->_account;                  }

    public function setPassword($string) {        $this->_password = md5($string); }
    public function getPassword()        { return $this->_password;                }
    public function setPurePassword($string) { $this->_password = $string;  }

    public function setAuthMethod($string) { $this->_authMethod = trim($string);  }
    public function getAuthMethod()        { return $this->_authMethod;       }

    public function setAuthType($int) { $this->_authType = (int) $int;  }
    public function getAuthType()     { return $this->_authType;       }

    public function setEmail($string) { $this->_email = trim($string);  }
    public function getEmail()        { return $this->_email;    }

    public function setEmailStatus($int) { $this->_emailStatus = (int) $int;  }
    public function getEmailStatus()     { return $this->_emailStatus;        }

    public function setStatus($int) { $this->_status = (int) $int;  }
    public function getStatus()     { return $this->_status;        }


    /* ------------------------------------------------------------------------------------------------------------------------
        extends
    ------------------------------------------------------------------------------------------------------------------------ */

    /**
     *  取得該 user 下的權限列表
     */
    function getUserPermissionCategories()
    {
        $userPermissionCategories = new UserPermissionCategories();
        return $userPermissionCategories->getUserPermissionCategoriesByUserId( $this->getId() );
    }


}

